package com.uxhao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uxhao.commonutils.R;
import com.uxhao.commonutils.entity.vo.CourseInfoCommonVo;
import com.uxhao.eduservice.entity.EduCourse;
import com.uxhao.eduservice.entity.frontvo.CourseFrontInfoVo;
import com.uxhao.eduservice.entity.query.CourseQuery;
import com.uxhao.eduservice.entity.vo.CourseInfoVo;
import com.uxhao.eduservice.entity.vo.CoursePublishVo;
import com.uxhao.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author uxhao
 * @since 2022/01/11
 */
@Api(tags={"课程接口"})
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @ApiOperation("添加课程")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",courseId);

    }
    @ApiOperation("获取课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo info= courseService.getCourseInfoById(courseId);

        return  R.ok().data(info);
    }

    @ApiOperation("更新课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfoById(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据课程id获取最终发布结果")
    @GetMapping("getCoursePublishVoById/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id){
        CoursePublishVo coursePublishVo= courseService.getCoursePublishVoById(id);
        return R.ok().data("item",coursePublishVo);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        boolean b = courseService.publishCourseById(id);
        return b?R.ok():R.error().message("课程发布失败");
    }

    @ApiOperation(value = "条件查询带分页方法")
    @GetMapping("pageListCourseCondition/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page, limit);

        courseService.pageQuery(pageParam, courseQuery);

        //TODO 显示课程下现有的章节和小节
        List<EduCourse> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = courseService.removeCourseById(id);

        return result? R.ok():R.error().message("删除失败");
    }


    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CourseInfoCommonVo getCourseInfoDto(@PathVariable String courseId) {
        CourseFrontInfoVo courseFrontInfoVo = courseService.selectInfoWebById(courseId);
        CourseInfoCommonVo courseInfo = new CourseInfoCommonVo();
        BeanUtils.copyProperties(courseFrontInfoVo,courseInfo);
        return courseInfo;
    }
}

